package com.firebird.app.fejpsl.ui.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.firebird.app.fejpsl.R
import com.firebird.app.fejpsl.constant.Constant
import com.firebird.app.fejpsl.ext.showToast
import com.firebird.app.fejpsl.ui.activity.LoginActivity
import com.firebird.app.fejpsl.ui.activity.me.MemberLevelActivity
import com.firebird.app.fejpsl.ui.activity.me.PersonInfoActivity
import com.firebird.app.fejpsl.ui.activity.me.SettingActivity
import com.firebird.app.fejpsl.ui.activity.me.StatsticalInfoActivity
import com.firebird.app.fejpsl.utils.Preference
import kotlinx.android.synthetic.main.fragment_me.*
import java.math.RoundingMode
import java.text.DecimalFormat

/**
 * A simple [Fragment] subclass.
 *
 */
class MeFragment : Fragment() {

    companion object {
        fun getInstance(): MeFragment = MeFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_me, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        refreshData()
        initListener()
    }

    override fun onResume() {
        super.onResume()
        //showToast("刷新数据1")
        refreshData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (MeFragment != null && !hidden) {
           // showToast("刷新数据2")
            refreshData()
        }
    }

    private fun refreshData(){
        val niceName:String by Preference(Constant.NICE_NAME_KEY, "")
        val realName:String by Preference(Constant.REAL_NAME_KEY, "")
        val userId:String by Preference(Constant.USER_ID_KEY, "")
        val isExplicit:Int by Preference(Constant.IS_EXPLICIT_KEY, 1)
        val area:String by Preference(Constant.AREA_KEY, "")
        val creditScore:Int by Preference(Constant.CREDIT_SCORE_KEY, 100)
        val copperCash:Long by Preference(Constant.COPPER_CASH_KEY, 0)
        val goldIngot:Long by Preference(Constant.GOLD_INGOT_KEY, 0)
        val levelScore:Long by Preference(Constant.LEVEL_SCORE_KEY,0)
        if(isExplicit==1){
            val tmp_str="["+getLevelName(levelScore)+"]"+niceName
            mUserName.setText(tmp_str)
        }else{
            val tmp_str="["+getLevelName(levelScore)+"]"+realName
            mUserName.setText(tmp_str)
        }
        mUserID.setText("ID:$userId")
        if (area.length>0){
            mArea.setText("地区:$area")
        }else{
            mArea.setText("地区:--")
        }
        val end_pos=4+creditScore.toString().length
        var style:SpannableStringBuilder= SpannableStringBuilder("信用分:$creditScore")
        style.setSpan( ForegroundColorSpan(Color.WHITE), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        style.setSpan( ForegroundColorSpan(Color.GREEN), 4, end_pos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        mCreditScore.setText(style)
        var tmp_str:String=""
        if (copperCash>10000) {
            tmp_str=getNoMoreThanTwoDigits( copperCash/ 10000.0)+"万"
        }else{
            tmp_str=copperCash.toString()
        }
        mCopperCash.setText(tmp_str)
        if (goldIngot>10000) {
            tmp_str=getNoMoreThanTwoDigits( goldIngot/ 10000.0)+"万"
        }else{
            tmp_str=goldIngot.toString()
        }
        mGoldIngot.setText(tmp_str)
    }

    private fun initListener() {
        mUserName.setOnClickListener {
            Intent(activity, PersonInfoActivity::class.java).run {
                startActivity(this)
            }
        }
        mUserID.setOnClickListener {
            Intent(activity, PersonInfoActivity::class.java).run {
                startActivity(this)
            }
        }
        mArea.setOnClickListener {
            Intent(activity, PersonInfoActivity::class.java).run {
                startActivity(this)
            }
        }
        mCreditScore.setOnClickListener {
            Intent(activity, PersonInfoActivity::class.java).run {
                startActivity(this)
            }
        }
        mLogin.setOnClickListener {
            Intent(activity, LoginActivity::class.java).run {
                startActivity(this)
            }
        }
        mMemberLevel.setOnClickListener {
            Intent(activity, MemberLevelActivity::class.java).run {
                startActivity(this)
            }
        }
        mStatisticalInfo.setOnClickListener {
            Intent(activity, StatsticalInfoActivity::class.java).run {
                startActivity(this)
            }
        }
        mCollection.setOnClickListener {
            showToast(getString(R.string.established_tip))
        }
        mSetting.setOnClickListener {
            Intent(activity, SettingActivity::class.java).run {
                startActivity(this)
            }
        }
        mHelpFeedback.setOnClickListener {
            showToast(getString(R.string.established_tip))
        }
        mRecommendToFriend.setOnClickListener {
            showToast(getString(R.string.established_tip))
        }
        mUploadLog.setOnClickListener {
            showToast(getString(R.string.established_tip))
        }

    }

    /**
     * OnClickListener  method 2
     */
    /*
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.mUserName , R.id.mUserID,R.id.mArea,R.id.mCreditScore-> {
                Intent(activity, PersonInfoActivity::class.java).apply {
                    startActivity(this)
                }
            }
        }
    }
    */

    /**
     * 对入参保留最多两位小数(舍弃末尾的0)，如:
     * 3.345->3.34
     * 3.40->3.4
     * 3.0->3
     */
    fun getNoMoreThanTwoDigits(number: Double): String {
        val format = DecimalFormat("0.##")
        //未保留小数的舍弃规则，RoundingMode.FLOOR表示直接舍弃。
        format.roundingMode = RoundingMode.FLOOR
        return format.format(number)
    }
    fun getLevelName(number: Long): String {
        var levelName=""
        when (number) {
            in 0..50 -> levelName="草民"
            in 50..150 -> levelName="县丞"
            in 150..350 -> levelName="县令"
            in 350..750 -> levelName="都尉"
            in 750..1550 -> levelName="校尉"
            in 1550..3150 -> levelName="常侍"
            in 3150..6350 -> levelName="中郎将"
            in 6350..12750 -> levelName="太守"
            in 12750..20000 -> levelName="刺史"
            in 20000..30000 -> levelName="将军"
            in 30000..40000 -> levelName="太尉"
            in 40000..50000 -> levelName="大将军"
            else -> levelName="王"
        }
        return levelName
    }
}
